PLUGIN.Name = "Metronome"
PLUGIN.DefaultOn = false
PLUGIN.Description = 
[[Just a metronome. Set its beat per minute and the sound to play.]]

function PLUGIN:LoadParameters()
	self:AddParameter("print", { Type = "checkbox", Defaults = "0", Text = "Print debug text in console" } )
	self:AddParameter("sound_label", { Type = "label", Text = "Sound to play :" } )
	self:AddParameter("sound", { Type = "textentry", Defaults = "buttons/blip1.wav" } )
	self:AddParameter("frequency", { Type = "slider", Defaults = "80", Min = "4", Max = "360", Decimals = "0", Text = "Beats per minute" } )
	self:AddParameter("stop", { Type = "button", Text = "Tick now", DoClick = function() self:_Sound() end } )
end

function PLUGIN:Load()
	self:_Sound()
	
	local freq = self:GetNumber("frequency")
	freq = (freq > 0) and freq or 1
	timer.Create("metronome", 60/freq, 0, self._Sound, self)
end

-- Functions that begin with a "_" are not automatically hooked.
function PLUGIN:_Sound()
	LocalPlayer():EmitSound( self:GetString("sound") )
	
	if (self:GetNumber("print") > 0) then print("Sound playing : " .. self:GetString("sound")) end
	
	local freq = self:GetNumber("frequency")
	freq = (freq > 0) and freq or 1
	timer.Adjust("metronome", 60/freq, 0, self._Sound, self)
end

function PLUGIN:Unload()
	timer.Remove("metronome")
end
